package org.dromara.book.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBaseBookTypeBo;
import org.dromara.book.domain.vo.SzlyBaseBookTypeVo;
import org.dromara.book.domain.SzlyBaseBookType;
import org.dromara.book.mapper.SzlyBaseBookTypeMapper;
import org.dromara.book.service.ISzlyBaseBookTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 图书分类Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-15
 */
@RequiredArgsConstructor
@Service
public class SzlyBaseBookTypeServiceImpl implements ISzlyBaseBookTypeService {

    private final SzlyBaseBookTypeMapper baseMapper;

    /**
     * 查询图书分类
     */
    @Override
    public SzlyBaseBookTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询图书分类列表
     */
    @Override
    public TableDataInfo<SzlyBaseBookTypeVo> queryPageList(SzlyBaseBookTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyBaseBookType> lqw = buildQueryWrapper(bo);
        Page<SzlyBaseBookTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询图书分类列表
     */
    @Override
    public List<SzlyBaseBookTypeVo> queryList(SzlyBaseBookTypeBo bo) {
        LambdaQueryWrapper<SzlyBaseBookType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyBaseBookType> buildQueryWrapper(SzlyBaseBookTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBaseBookType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), SzlyBaseBookType::getCode, bo.getCode());
        lqw.eq(bo.getParentId() != null,SzlyBaseBookType::getParentId,bo.getParentId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyBaseBookType::getName, bo.getName());
        lqw.eq(bo.getSort() != null, SzlyBaseBookType::getSort, bo.getSort());
        lqw.orderByAsc(SzlyBaseBookType::getCode);
        return lqw;
    }

    /**
     * 新增图书分类
     */
    @Override
    public SzlyBaseBookTypeBo insertByBo(SzlyBaseBookTypeBo bo) {
        SzlyBaseBookType add = MapstructUtils.convert(bo, SzlyBaseBookType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return bo;
    }

    /**
     * 修改图书分类
     */
    @Override
    public Boolean updateByBo(SzlyBaseBookTypeBo bo) {
        SzlyBaseBookType update = MapstructUtils.convert(bo, SzlyBaseBookType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBaseBookType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除图书分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public SzlyBaseBookTypeVo queryByCode(String text) {
        LambdaQueryWrapper<SzlyBaseBookType> wra = new LambdaQueryWrapper<>();
        wra.eq(SzlyBaseBookType::getCode,text);
        return baseMapper.selectVoOne(wra);
    }
}
